const express = require('express');
const request = require("../util/request");
const router = express.Router();

const send = (res,data) =>{
    if (!data) {
        res.send({code: 500, msg: '无数据响应'})
        return
    }
    if(data.error){
        res.send({code: 500, msg: `请求服务错误：${data.msg}`})
        return
    }
    res.send({code:200,data: data.data, headers: data.headers})
}

const sendHttp = (url, config, method = 'post') => {
    return request({
        baseURL: config.baseURL,
        method,
        headers: {...config.header},
        params: config.params,
        data: config.body,
        url,
    })
}

router.post('/api/watch', async function (req, res, next) {
    send(res,await sendHttp('/device/info/watch', req.body))
});

router.post('/api/downloadPath', async function (req, res, next) {
    send(res,await sendHttp('/device/download_paths', req.body,'get'))
});

router.post('/api/files', async function (req, res, next) {
    send(res,await sendHttp('/drive/v1/files', req.body,'get'))
});
router.post('/api/task', async function (req, res, next) {
    send(res,await sendHttp('/drive/v1/task', req.body))
});
router.post('/api/tasks', async function (req, res, next) {
    send(res,await sendHttp('/drive/v1/tasks', req.body,'get'))
});


module.exports = router;
